# -*- coding: UTF-8 -*-
# https://blog.csdn.net/xufive/article/details/82665460
#实现Frame之间的跳转  https://blog.csdn.net/tianmaxingkong_/article/details/53326463
import wx,multiprocessing,threading
from picmap.gui.mapFrame import *
from picmap.gui.picListFrame import *
from picmap.gui.pathListFrame import *

class window():
        instance=None
        def __init__(self,q):
            self.frameDict={}
            self.app = wx.App()
#            frame=  wx.Frame(None,-1,"地图")
            self.frame=self.Get('map')
#            self.frame.Show(True)
#            self.frame=self.Get('pathList')
#            self.frame.Show(True)
            window.instance=self
#            multiprocessing.Process(target=ThreadGuiQMsg,args=(self.frame,q)).start()
            StayAccept(self,q).start()
            
        def start(self):
#            openPicWindow('22.51,114.0')
            self.app.MainLoop()
            
            
        def Get(self, type):
            frame = self.frameDict.get(type)
            if frame is None:
                name=type+"Frame(self)"
                print(name)
                frame = eval(name)
                self.frameDict[type] =frame
                
            return frame

        def confirm(self,msg,title="提示"):
            confirm=wx.MessageDialog(self.frame,msg,title,wx.YES_NO|wx.ICON_WARNING)
            confirm.SetOKCancelLabels("确定","取消")
            return confirm.ShowModal() 

        def alert(self,msg,title='提示',style=0):
            wx.MessageDialog(self.frame,msg,title,style).ShowModal()

        #http://blog.sina.com.cn/s/blog_62eb178a0102vp5c.html
        def  promtp(self):
            wx.TextEntryDialog().ShowModal()
            
        def run(self,func,params=0):
            print("call gui run %s,%s" % (func,params))
            funcMap={
                "openPicWindow":openPicWindow,
                "openConfig":lambda parameter_list:  self.Get('pathList').Show(True),
                'openMain':lambda parameter_list:  self.Get('map').Show(True)
            }
            funcMap[func](params)
            pass
            
def openPicWindow(id):
    frame=picListFrame(window.instance)
    frame.ToggleWindowStyle(wx.STAY_ON_TOP)
    frame.show(id)
    frame.SetFocus()
    
    
def setStatusBar(msg='',num=''):
    print([msg,num])
    frame=window.instance.Get("map").setStatus("%s\t%s" % (msg,num))
    
#https://www.cnblogs.com/new-june/p/10783177.html
def ThreadGuiQMsg(window,Queue):
        wx.CallAfter(window.setStatus, '123344')
        while True:
            [func,params]=Queue.get()
            print("tget %s,%s" %(func,params))
            wx.CallAfter(window.setStatus, func,params)
            print("tget %s,%s end" %(func,params))
            

class StayAccept(threading.Thread):
    def __init__(self, window,Queue):
        super(StayAccept, self).__init__()
        self.window = window
        self.queue = Queue

    def run(self) -> None:
        while True:
            if self.queue == None:
                exit()
            [func,params]=self.queue.get()
            wx.CallAfter(self.window.run, func,params)